function init()
{
	initListeners();
	
	initLoginPopup();
	
	 var normalColor = $('a:link').css('color');
     $('a:visited').css('color', normalColor);
}

function initListeners()
{
	console.log("Init listeners");
	
		
	$( '.submission_item' ).hover(
			  function() {
			    $( this ).addClass( "highlight" );
			  }, function() {
			    $( this ).removeClass( "highlight" );
			  }
			);
	
	$('.submission_item').on('click',function(event){

		var targetid = $(this).attr("id");

		//$( "#dialog-confirm" ).html($( "#dialog-confirm" ).html()+"<br/>"+)
		$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:250,
			width:550,
			modal: true,
			show:"fast",
			dialogClass: "no-close",
			closeOnEscape: false,
			draggable: false,
			buttons: {
				"Ok": function() {
					$( this ).dialog( "close" );


					//call async MAC offers controller to format email with the target offer
					$.ajax({
						type: 'POST',
						url: '/offers/sendoffer', // L'url vers laquelle la requete sera envoyee
						data: "offer_id="+targetid, 
						success: function(data, textStatus, jqXHR) {
						
							$( "#dialog-success" ).dialog({	resizable: false,
															height:250,
															width:550,
															modal: true,
															show:"fast",
															dialogClass: "no-close",
															closeOnEscape: false,
															draggable: false});
							
						
						},
						error: function(jqXHR, textStatus, errorThrown) {
							
							$( "#dialog-error" ).dialog({	resizable: false,
															height:250,
															width:550,
															modal: true,
															show:"fast",
															dialogClass: "no-close",
															closeOnEscape: false,
															draggable: false});
							
						}
					});

					//then, display the recap email in a popup to the user



					//on "yes" click, send the email to admin contact, cancel otherwise


				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			}
		});




		event.preventDefault();
		return false;
	});
}

function initLoginPopup(){
	$('a.login-window').click(function() {
		var loginBox = $(this).attr('href');

		$(loginBox).fadeIn(300);

		var popMargTop = ($(loginBox).height() + 24) / 2; 
		var popMargLeft = ($(loginBox).width() + 24) / 2; 

		$(loginBox).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});

		$('body').append('<div id="mask"></div>');
		$('#mask').fadeIn(300,function(){
			$('#username').focus();
		});

		return false;
	});

	$('a.close, #mask').on('click', function() { 
		$('#mask , .login-popup').fadeOut(300 , function() {
			$('#mask').remove();  
		}); 
		return false;
	});
	
	$('.account-box').on('click',function(e){
		var submenu = $('#account_menu');

//		submenu.width($('#application_menu').width());//popup same width as menu
		
	    if (submenu.is(":visible")) {
	    	submenu.fadeOut();
	    } else {
	        submenu.fadeIn();
	    }
		
		e.preventDefault();
		return false;
	});
	
	//hover and active account menu management
	var submenu_active = false;
	 
	$('#account_menu').mouseenter(function() {
	    submenu_active = true;
	});
	 
	$('#account_menu').mouseleave(function() {
	    submenu_active = false;
	    setTimeout(function() { if (submenu_active === false) $('#account_menu').fadeOut(); }, 400);
	});
}